//
//  ViewController.m
//  上传
//
//  Created by student on 16/3/30.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#define KBoundary @"laoziniua"
#define KNewLine @"\r\n"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self uploadfile];
}
//1
//------WebKitFormBoundaryeMid9pd6h040Q7xB
//Content-Disposition: form-data; name="file"; filename="35a85edf8db1cb13b57d9466db54564e92584bab.jpg"
//Content-Type: image/jpeg
//2
//文件内容
//3
//------WebKitFormBoundaryeMid9pd6h040Q7xB
//Content-Disposition: form-data; name="username"
//
//abc
//4
//------WebKitFormBoundaryeMid9pd6h040Q7xB--

-(void)uploadfile
{
    NSURL *url=[NSURL URLWithString:@"http://192.168.13.13:8080/MJServer/upload"];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod=@"POST";
    NSString *headerstr=[NSString stringWithFormat:@"multipart/form-data; boundary=%@",KBoundary];
    [request setValue:headerstr forHTTPHeaderField:@"Content-Type"];
    
    
    NSMutableData *mdata=[NSMutableData data];
    NSMutableString *mstr=[NSMutableString string];
    
    
    [mstr appendString:KNewLine];
    [mstr appendString:@"--"];
    [mstr appendString:KBoundary];
    [mstr appendString:KNewLine];
    [mstr appendString:@"Content-Disposition: form-data; name=\"file\"; filename=\"35a85edf8db1cb13b57d9466db54564e92584bab.jpg\""];
    [mstr appendString:KNewLine];
    [mstr appendFormat:@"Content-Type: image/jpeg"];
    [mstr appendFormat:KNewLine];
    [mstr appendString:KNewLine];
    [mdata appendData:[mstr dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *filepath=[[NSBundle mainBundle]pathForResource:@"35a85edf8db1cb13b57d9466db54564e92584bab" ofType:@"jpg"];
    NSData *data=[NSData dataWithContentsOfFile:filepath];
    [mdata appendData:data];
    
    mstr=[NSMutableString string];
    [mstr appendString:KNewLine];
    [mstr appendString:@"--"];
    [mstr appendString:KBoundary];
    [mstr appendString:KNewLine];
    [mstr appendString:@"Content-Disposition: form-data; name=\"username\""];
    [mstr appendString:KNewLine];
    [mstr appendString:KNewLine];
    [mstr appendString:@"abc"];
    [mstr appendString:KNewLine];
    
    [mstr appendString:@"--"];
    [mstr appendString:KBoundary];
    [mstr appendString:@"--"];
    [mstr appendString:KNewLine];
    
    [mdata appendData:[mstr dataUsingEncoding:NSUTF8StringEncoding]];
    request.HTTPBody=mdata;
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if(connectionError==nil)
        {
            NSError *error;
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
            if(error!=nil)
            {
                NSLog(@"%@",dict);
            }
            else
            {
                NSLog(@"%@",[error localizedDescription]);
            }
        }
        else
        {
            NSLog(@"%@",[connectionError localizedDescription]);
        }
    }];

}
@end
